package hk.com.fwd.biz;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import hk.com.fwd.biz.assembler.PolicyItemAdjustmentSearchResultAssembler;
import hk.com.fwd.biz.exception.DmsException;
import hk.com.fwd.biz.util.TransformFiledUtil;
import hk.com.fwd.dao.PolicyItemAdjustmentDao;
import hk.com.fwd.entity.PolicyItemAdjustmentSearchResult;
import hk.com.fwd.model.PageBean;
import hk.com.fwd.model.PolicyItemAdjustmentSearchResultDTO;
import hk.com.fwd.model.SubPageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;


@Service
@Transactional
public class PolicyItemAdjustmentTestBo extends BaseBo {

   @Autowired
   private PolicyItemAdjustmentDao policyItemAdjustmentDao;

   private static Logger logger = LogManager.getLogger(hk.com.fwd.biz.PolicyItemAdjustmentTestBo.class);

   private final static  String [] ARRAYFILED={"acspda_agent_no","agent_name","camaid_channel_code","acspda_policy_no","linedesc","acspda_type","acspda_adj_date","acspda_coverage","acspda_action","approval_status_display"};

   private final static Long MAXCOUNT=200L;



    public PageInfo searchPolicyItemAdjustment(PageBean<PolicyItemAdjustmentSearchResultDTO> pageBean)throws DmsException {
        logger.info("Start searchPolicyItemAdjustment");
      if(  StringUtils.isEmpty(pageBean.getPageData().getInternalAgentNo())&&
              StringUtils.isEmpty(pageBean.getPageData().getChannelCode())&&
              StringUtils.isEmpty(pageBean.getPageData().getLine())&&
              pageBean.getPageData().getCoverage()== null&&
              StringUtils.isEmpty(pageBean.getPageData().getType())&&
              pageBean.getPageData().getAdjDate() == null &&
              StringUtils.isEmpty(pageBean.getPageData().getPolicyNo()) &&
              StringUtils.isEmpty(pageBean.getPageData().getApprovalStatus())
                ) {
            throw new DmsException("E-PAI-007");
        }
            PolicyItemAdjustmentSearchResultDTO policyItemAdjustmentSearchResultDTO=pageBean.getPageData();
            PolicyItemAdjustmentSearchResult po= PolicyItemAdjustmentSearchResultAssembler.toEntity(policyItemAdjustmentSearchResultDTO);
            PolicyItemAdjustmentSearchResult totalRecordResult=policyItemAdjustmentDao.getPolicyItemAdjustmentListCount(po);
            Long totalRecord=totalRecordResult.getTotalRecord();
            logger.info("search total totalRecord: "+ totalRecord);
            if(totalRecord>MAXCOUNT){
                throw new DmsException("E-PAI-009");
            }

        Page page = SubPageHelper.startPage(pageBean.getPageNum(), pageBean.getPageSize(), TransformFiledUtil.transformDbModelFiled(pageBean.getSortStr(),ARRAYFILED));
        List<PolicyItemAdjustmentSearchResult>  policyItemAdjustmentSearchResultList=policyItemAdjustmentDao.getPolicyItemAdjustmentList(po);

        PageInfo info = new PageInfo<>(page.getResult());
        info.setList(PolicyItemAdjustmentSearchResultAssembler.toDTOs(policyItemAdjustmentSearchResultList));
        return info;

    }





}
